package com.yitianyike.lockscreen.ces.bo.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yitianyike.lockscreen.ces.bo.CompleteBO;
import com.yitianyike.lockscreen.ces.dao.ColumnDAO;
import com.yitianyike.lockscreen.ces.dao.DataCacheDAO;
import com.yitianyike.lockscreen.ces.dao.PackageDAO;
import com.yitianyike.lockscreen.ces.dao.RedisDAO;
import com.yitianyike.lockscreen.ces.model.Column;
import com.yitianyike.lockscreen.ces.model.PackageInfo;
import com.yitianyike.lockscreen.ces.service.DataAccessFactory;
import com.yitianyike.lockscreen.ces.util.PropertiesUtil;

@Component("completeBO")
public class CompleteBOImpl implements CompleteBO {

//	private static Logger logger = Logger.getLogger(CompleteBOImpl.class.getName());
	
	private List<Map<String , Object>> reslist = new ArrayList<Map<String,Object>>();
	
	@Autowired
	private PackageDAO packageDAO ;
	@Autowired
	private ColumnDAO columnDAO;
	
	private DataCacheDAO dataCacheDAO = (DataCacheDAO) DataAccessFactory.dataHolder().get("dataCacheDAO");
	private RedisDAO redisDAO = (RedisDAO) DataAccessFactory.dataHolder().get("redisDAO");
	
	
	private String channelCode;
	private String version;
	
	@Override
	public List<Map<String, Object>> getComplete(String channel_code) {
		reslist.clear();
		channelCode = channel_code; 
		version = PropertiesUtil.version; 
		List<PackageInfo> packageList = packageDAO.getPackage(0 , channelCode );
		List<Column> columnList = columnDAO.getColumnByPackageId(0 , channelCode );
		Map<String , Object> map_p = new HashMap<String, Object>();
		map_p.put("pid", 0);
		map_p.put("name", "初始层");
		map_p.put("child", OrganizationChild(packageList , columnList));
		map_p.put("type", "0");
		reslist.add(map_p);
		OrganizationAll(packageList , columnList);
		JSONArray fromObject = JSONArray.fromObject(reslist);
		String cacheKey = channelCode+"-"+version+"-complete";
		dataCacheDAO.insertRecommendSubscribeList(cacheKey , fromObject.toString());
		redisDAO.removeRedisByCacheKey(cacheKey);
		return reslist;
	}
	
	public void OrganizationAll(List<PackageInfo> listp , List<Column> listc){
		for (PackageInfo packageInfo : listp) {
			Map<String , Object> map_p = new HashMap<String, Object>();
			List<PackageInfo> packageList =	packageDAO.getPackage(packageInfo.getPackageId() , channelCode );
			List<Column> columnList = columnDAO.getColumnByPackageId(packageInfo.getPackageId() , channelCode );
			if(packageList.isEmpty() && columnList.isEmpty()){
				break ; 
			}
			map_p.put("pid", packageInfo.getPackageId());
			map_p.put("name", packageInfo.getPackageName());
			map_p.put("child", OrganizationChild(packageList , columnList));
			map_p.put("type", "1");
//			reslist.add(map_c);
			reslist.add(map_p);
			if(!packageList.isEmpty()){
				OrganizationAll(packageList , columnList);
			}
		}
	}
	
	public List<Map<String , Object>> OrganizationChild(List<PackageInfo> listp , List<Column> listc){
		List<Map<String , Object>> reslist = new ArrayList<Map<String,Object>>();
		if(listp!=null){
			for (PackageInfo packageInfo : listp) {
				Map<String , Object> list_action = new HashMap<String, Object>();
				Map<String , Object> preference = new HashMap<String, Object>();
				list_action.put("list_skip_url", "");
				list_action.put("list_skip_action", "");
				Map<String , Object> map = new HashMap<String, Object>();
				map.put("list_action", list_action);
				ArrayList arr = new ArrayList();
				map.put("param", arr);
				map.put("dataStyle", packageInfo.getPackageStyle());
				map.put("data", arr);
				map.put("type", 0);
				map.put("littleType", 0);
				map.put("aid", "");
				map.put("pid", packageInfo.getPackageId());
				map.put("level", 0);
				map.put("endTime", packageInfo.getEndTime());
				map.put("startTime", packageInfo.getStartTime());
				preference.put("icon", packageInfo.getIcon());
				preference.put("name",  packageInfo.getPackageName());
				map.put("preference", preference);
				reslist.add(map);
			}
		}
	
		if(listc!=null){
			for (Column column : listc) {
				Map<String , Object> list_action = new HashMap<String, Object>();
				Map<String , Object> preference = new HashMap<String, Object>();
				list_action.put("list_skip_url", column.getH5Url()+"");
				list_action.put("list_skip_action", column.getSkipAction()+"");
				Map<String , Object> map = new HashMap<String, Object>();
				map.put("list_action", list_action);
				ArrayList arr = new ArrayList();
				map.put("param", arr);
				map.put("dataStyle", column.getColumnStyle());
				map.put("data", arr);
				map.put("type", column.getType());
				map.put("littleType", column.getLittleType());
				map.put("aid", column.getId()+"");
				map.put("pid", column.getPackageId());
				map.put("level", 1);
				map.put("endTime", column.getEndTime());
				map.put("startTime", column.getStartTime());
				preference.put("icon", column.getIcon());
				preference.put("name",  column.getColumnName());
				map.put("preference", preference);
				reslist.add(map);
			}
			
		}
		return reslist ;
	}
	
}
